$(function(){
	var index=0;
	//点击图片放大显示
	$(".r-two-small1").each(function(i,e){
		$(e).click(function(){
			
		$('.r-two-big img').attr('src',$(e).attr('src'))	;
		$('.r-two-big3 img').attr('src',$(e).attr('src'));	
//			console.log($('.r-two-big').attr('src'));
			
			$(".r-two-small1").removeClass("r-two-one");
			$(".r-two-small1").eq(i).addClass("r-two-one");
			
		})
		
	})
	//点击左右按钮
	//点击左按钮
	$(".r-two-jian:eq(0)").click(function(){
		if(index<=0){
			index=3;
		}else{
			index--;
		}
			$(".r-two-small1").removeClass("r-two-one");
			$(".r-two-small1").eq(index).addClass("r-two-one");
			//展示区域和放大区域
			$('.r-two-big img').attr('src',$(".r-two-small1").eq(index).attr('src'))	;
		    $('.r-two-big3 img').attr('src',$(".r-two-small1").eq(index).attr('src'));	
			

	})
	//点击右键
	
	$(".r-two-jian:eq(1)").click(function(){
		if(index>=3){
			index=0
		}else{
			index++;
		}
			$(".r-two-small1").removeClass("r-two-one");
			$(".r-two-small1").eq(index).addClass("r-two-one");
			//展示区域和放大区域
			$('.r-two-big img').attr('src',$(".r-two-small1").eq(index).attr('src'))	;
		    $('.r-two-big3 img').attr('src',$(".r-two-small1").eq(index).attr('src'));	
			
	   });
//放大镜
        var small = document.getElementsByClassName("r-two-big")[0];
		var big = document.getElementsByClassName("r-two-big3")[0];
		var zoom = document.getElementsByClassName("r-zoom")[0];
		var bigImg = document.getElementsByClassName("r-two-hidden")[0];  small.onmousemove = function(e){
			var even = e || event;
//			console.log($('.r-two-big').offset().left);
			var x = even.clientX - zoom.offsetWidth/2 - $('.r-two-big').offset().left;
			var y = even.clientY - zoom.offsetHeight/2 - $('.r-two-big').offset().top;
			//水平方向的left最大临界值
			var maxX = small.clientWidth - zoom.clientWidth;
			if(x < 0){
				x = 0
			}
			if(x > maxX){
				x = maxX
			}
			//竖直方向的top最大临界值
			var maxY = small.clientHeight - zoom.clientHeight;
			if(y < 0){
				y = 0
			}
			if(y > maxY){
				y = maxY
			}
			zoom.style.left = x + "px";
			zoom.style.top = y + "px";
			console.log(bigImg.offsetWidth);
			var bigImgX = (x / maxX) * (bigImg.offsetWidth - big.clientWidth);
			var bigImgY = (y / maxY) * (bigImg.offsetHeight - big.clientHeight);
			//切记要给小的赋值,不能给bigImg.scrollLeft = bigImgX 这样写
			big.scrollLeft = bigImgX;
			big.scrollTop = bigImgY;
		}
		
		
//		
		//为小图区域添加鼠标移入移出事件
		 small.onmouseenter = function(){
		 	
			zoom.style.display = "block";
			big.style.display = "block";
			
//			$('.r-two-big img').each(function(i,e){
//				if(!$(e).is(':hidden')){
//					$('.r-two-big3').find('img').attr('src',$(e).attr('src'));
//				}
//			});
			
			
			
		}
		small.onmouseleave = function(){
			zoom.style.display = "none";
			big.style.display = "none";
			
		}


//右边点击字体变色
$(".r-two-write a").each(function(inde,ele){
	$(ele).click(function(){
		$(".r-two-write a").removeClass("r-two-write1");
		$(".r-two-write a").eq(inde).addClass("r-two-write1");
		
		
	})
	
})

//选择重量
$(".r-two-middle1 a").each(function(inde,ele){
	$(ele).click(function(){
		$(".r-two-middle1 a").removeClass("r-two-write2");
		$(".r-two-middle1 a").eq(inde).addClass("r-two-write2");
		
		
	})

	
   })
//购物车加减数量

var i = 1;
//点击减号
$(".r-two-math button").eq(0).click(function(){
	console.log(222);
	if (i == 1) return;
	$(".r-two-input").val(--i);
//		i = oSpan.value-0;
//		if (i == 1) return;
//		oSpan.value = --i
//		console.log(oSpan.innerHTML);
	});
	
//点击加号
$(".r-two-math button").eq(1).click(function(){
	console.log(111);
//	i++;
	$(".r-two-input").val(++i);
//	  i = oSpan.value-0;
//		oSpan.value = ++i
	});
	
	
//点击商品详情
$(".r-two-head1").each(function(index,element){
	$(element).click(function(){
		$(".r-two-head1").removeClass("r-two-h");
		$(".r-two-head1").eq(index).addClass("r-two-h");
		$(".r-two-details").removeClass("r-two-data");
		$(".r-two-details").eq(index).addClass("r-two-data");
		
	})
	
})
//

	
	
	
	
})
